package com.extr.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.extr.domain.practice.PracticeRecord;
import com.extr.service.PracticeRecordService;
import com.extr.util.Page;
import com.extr.util.PagingUtil;


/**
 * @author Ocelot
 * @date 2014年7月29日 下午4:30:18
 */
@Controller
public class PracticeRecordController {

	@Autowired
	public PracticeRecordService recordService;

	private static Logger log = Logger.getLogger(PracticeRecordController.class);

	/**
	 * 获取用户的练习记录
	 * @return
	 */
	@RequestMapping(value = "/admin/user-record", method = RequestMethod.GET)
	public String getPracticeRecord(Model model, HttpServletRequest request){
		
		model.addAttribute("pageStr", "");
		model.addAttribute("startTime", "");
		model.addAttribute("endTime", "");
		model.addAttribute("recordList", new ArrayList<PracticeRecord>());
		return "admin/user-record-list"; 
	}
	
	@RequestMapping(value = "/admin/user-practice-record/{startTime}/{endTime}/{page}", method = RequestMethod.GET)
	public String getUserRecord(Model model, HttpServletRequest request,
			@PathVariable("startTime") String startTime,
			@PathVariable("endTime") String endTime,
			@PathVariable("page") int page){

		String userName = request.getParameter("userName");
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("userName", userName);
		
		Page<PracticeRecord> pageModel = new Page<PracticeRecord>();
		pageModel.setPageNo(page);
		pageModel.setPageSize(20);
		
		List<PracticeRecord> recordList = recordService.getPracticeRecordByTime(map, pageModel);
		String pageStr = PagingUtil.getPageBtnlink(page, pageModel.getTotalPage());
		
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		model.addAttribute("userName", userName);
		model.addAttribute("pageStr", pageStr);
		model.addAttribute("recordList", recordList);
		return "admin/user-record-list"; 
	}
}
